/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package service;

import entities.Comment;
import java.util.Collection;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.persistence.EntityManager;
import converter.CommentsConverter;
import converter.CommentConverter;
import javax.persistence.PersistenceContext;
import javax.ejb.Stateless;
import javax.persistence.Query;

/**
 *
 * @author Alex
 */

@Path("/comments/")
@Stateless
public class CommentsResource {
    @javax.ejb.EJB
    private CommentResource commentResource;
    @Context
    protected UriInfo uriInfo;
    @PersistenceContext(unitName = "uTaskPU")
    protected EntityManager em;
  
    /** Creates a new instance of CommentsResource */
    public CommentsResource() {
    }

    /**
     * Get method for retrieving a collection of Comment instance in XML format.
     *
     * @return an instance of CommentsConverter
     */
    @GET
    @Produces({"application/xml", "application/json"})
    public CommentsConverter get(@QueryParam("start")
            @DefaultValue("0") int start, @QueryParam("max")
            @DefaultValue("10") int max, @QueryParam("expandLevel")
            @DefaultValue("1") int expandLevel, @QueryParam("query")
            @DefaultValue("SELECT e FROM Comment e") String query) {
        return new CommentsConverter(getEntities(start, max, query), uriInfo.getAbsolutePath(), expandLevel);
    }

    /**
     * Post method for creating an instance of Comment using XML as the input format.
     *
     * @param data an CommentConverter entity that is deserialized from an XML stream
     * @return an instance of CommentConverter
     */
    @POST
    @Consumes({"application/xml", "application/json"})
    public Response post(CommentConverter data) {
        Comment entity = data.resolveEntity(em);
        long maxId = 0;
        try {
            maxId = (Long)em.createQuery("SELECT MAX(n.id) FROM Comment n").getSingleResult();
        } catch (Exception ex) { }
        entity.setId(maxId+1);
        
        createEntity(entity);
        return Response.created(uriInfo.getAbsolutePath().resolve(entity.getId() + "/")).build();
    }

    /**
     * Returns a dynamic instance of CommentResource used for entity navigation.
     *
     * @return an instance of CommentResource
     */
    @Path("{id}/")
    public CommentResource getCommentResource(@PathParam("id")
    Long id) {
        commentResource.setId(id);
        commentResource.setEm(em);
        return commentResource;
    }

    @GET
    @Produces({"application/xml", "application/json"})
    @Path("noteId={noteId}/")
    public CommentsConverter getCommentsForNotes(@PathParam("noteId") Long id) {
        Query q = em.createNamedQuery("Comment.findByIdNote").setParameter("idNote", id);
        return new CommentsConverter(q.getResultList(), uriInfo.getAbsolutePath(), 1);
    }

    /**
     * Returns all the entities associated with this resource.
     *
     * @return a collection of Comment instances
     */
    protected Collection<Comment> getEntities(int start, int max, String query) {
        return em.createQuery(query).setFirstResult(start).setMaxResults(max).getResultList();
    }

    /**
     * Persist the given entity.
     *
     * @param entity the entity to persist
     */
    protected void createEntity(Comment entity) {
        em.persist(entity);
    }
}
